﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using com.yd.sdk.api;
using UnityEditor;
using UnityEngine;

namespace com.yd.sdk
{
    // [EditorWindowTitle(title = "Project", icon = "Project")]
    public class SDKEditorWindow : EditorWindow
    {
        private ISetting _adSetting, _paySetting, _analysisSetting, _loginSetting, _otherSetting;
        private static string title = "SDK配置";
        private Vector2 _windowsScrollPosition;
        public static SdkSettings sdkSettings;
        
        [MenuItem("YD_SDK_MANAGER/SDK_SETTING")]
        public static void Show()
        {
            var window = GetWindow<SDKEditorWindow>(false, title);
            window.Init();
        }

        public void Init()
        {
            sdkSettings = AssetDatabase.LoadAssetAtPath<SdkSettings>("Assets/Resources/SDK_CONFIG.asset");
            _adSetting = new AdSetting();
            _paySetting = new PaySetting();
            _analysisSetting = new AnalysisSetting();
            _loginSetting = new LoginSetting();
            _otherSetting = new OtherSetting();
            _adSetting.Init();
            _paySetting.Init();
            _analysisSetting.Init();
            _loginSetting.Init();
            _otherSetting.Init();
        }

        private void OnGUI()
        {
            _windowsScrollPosition = EditorGUILayout.BeginScrollView(_windowsScrollPosition);
            _adSetting.OnGUI();
            _paySetting.OnGUI();
            _analysisSetting.OnGUI();
            _loginSetting.OnGUI();
            _otherSetting.OnGUI();
            EditorGUILayout.EndScrollView();
            
            // EditorGUILayout.Space();
            //
            // DrawWindow();
            //
            // EditorGUILayout.Space();
            //
            // GUI.enabled = true;

            GUILayout.Label("支付SDK模块");
            if (GUILayout.Button(EditorGUIUtility.IconContent("StepButton"), GUILayout.Width(50), GUILayout.Height(30)))
                Apply();

        }


        private void Apply()
        {
            if (isUseSDK())
                InstallYD_SDK();
            else
                RemoveYD_SDK();

            // _loginSetting.Apply();
            _paySetting.Apply();
            // _adSetting.Apply();
            // _analysisSetting.Apply();
             _otherSetting.Apply();
            
            // AssetDatabase.ImportAsset ("Assets/Resources/SDK_CONFIG.asset");
            // AssetDatabase.CreateAsset(sdkSettings,"Assets/Resources/SDK_CONFIG.asset");
            // AssetDatabase.Refresh();
        }

        /// <summary>
        /// 安装SDK
        /// </summary>
        private void InstallYD_SDK()
        {
            //尝试初始化设置
            Utils.CopyAndroidManifest(Constants.GetFullPath(Constants.ORIGIN_ANDROID_MANIFEST,true)
                ,Constants.GetFullPath(Constants.ANDROID_MAIN_MANIFEST));
            
            //覆盖Gradle文件
            if(!File.Exists(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH)))
                FileUtil.ReplaceFile(Constants.GetFullPath(Constants.ORIGIN_MAIN_TEMPLATE_GRADLE,true), Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH));
            
            //添加SDK:Main插件
            Utils.MergeDependenciesInGradle(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                Constants.ANDROID_MAIN_SDK_DEPENDENCIES);
          
            //使用3.4.3 gradle,但实际上用的是6.9因为
            if (!File.Exists(Constants.GetFullPath(Constants.ANDROID_BASE_TEMPLATE_PATH)))
                FileUtil.ReplaceFile(Constants.GetFullPath(Constants.ORIGIN_BASE_TEMPLATE,true), Constants.GetFullPath(Constants.ANDROID_BASE_TEMPLATE_PATH));
            
            //修改JDK11
            if (!File.Exists(Constants.GetFullPath(Constants.ANDROID_GRADLE_TEMPLATE_PATH)))
                FileUtil.ReplaceFile(Constants.GetFullPath(Constants.ORIGIN_GRADLE_TEMPLATE_PATH,true), Constants.GetFullPath(Constants.ANDROID_GRADLE_TEMPLATE_PATH));
            
            //PROGUARD
            if(!File.Exists(Constants.GetFullPath(Constants.ANDROID_PROGUARD_USER_PATH)))
                FileUtil.ReplaceFile(Constants.GetFullPath(Constants.ORIGIN_PROGUARD_USER_PATH,true), Constants.GetFullPath(Constants.ANDROID_PROGUARD_USER_PATH));
            
        }

        /// <summary>
        /// 卸载SDK
        /// </summary>
        private void RemoveYD_SDK()
        {
            //默认不会删除AndroidManifest.xml
            Utils.ResetAndroidManifest();
            //mainTemplate.gradle保留 删除SDK:Main的依赖
            Utils.DeleteStringFromFile(Constants.GetFullPath(Constants.ANDROID_MAIN_TEMPLATE_PATH),
                            "\t" + Constants.ANDROID_MAIN_SDK_DEPENDENCIES + "\r");
            
            //直接删除baseProjectTemplate.gradle
            if (File.Exists(Constants.GetFullPath(Constants.ANDROID_BASE_TEMPLATE_PATH)))
                File.Delete(Constants.GetFullPath(Constants.ANDROID_BASE_TEMPLATE_PATH));

            //直接删除gradleTemplate.properties
            if (File.Exists(Constants.GetFullPath(Constants.ANDROID_GRADLE_TEMPLATE_PATH)))
                File.Delete(Constants.GetFullPath(Constants.ANDROID_GRADLE_TEMPLATE_PATH));
        }

        /// <summary>
        /// 判断是否有引用本SDK中的模块
        /// </summary>
        /// <returns></returns>
        private bool isUseSDK()
        {
            return _loginSetting.IsSelectedSomething() || _paySetting.IsSelectedSomething() ||
                   _adSetting.IsSelectedSomething() ||
                   _analysisSetting.IsSelectedSomething() || _otherSetting.IsSelectedSomething();
        }
        
    }



}
